/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package SerialCom;

//~--- non-JDK imports --------------------------------------------------------

import gnu.io.*;

//~--- JDK imports ------------------------------------------------------------

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import java.io.InputStream;
import java.io.OutputStream;

import java.util.Vector;

import javax.swing.SwingWorker;
import javax.swing.table.*;

/**
 *
 * @author sf
 */
public class MainClass extends SwingWorker<String, Object> {
    static private RxCommPack evtRxCommPack;

    // Thread runner;

    /** Field description */
    static public NewJFrame njf;

    /** Field description */
    static Thread readThread;

    /**
     * Field description
     */
    InputStream inputStream;

    /**
     * Field description
     */
    OutputStream outputStream;

    /**
     * Field description
     */
    CommPortIdentifier portId;

    /**
     * Field description
     */
    MainClass pubMain;

    /** Field description */
    public RxCommPack rxCommPack;

    /** Field description */
    public String stringUpdate;

    @Override
    public String doInBackground() {

        return "test";
    }

    @Override
    protected void done() {
        try {
            System.out.println("arrivati al done");
        } catch (Exception ignore) {}
    }

    public static void main(String[] args) {

        Thread t = Thread.currentThread();

        t.setName("Thread Main");
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                njf = new NewJFrame();
                njf.setVisible(true);
            }
        });

        MainClass worker = new MainClass();

        worker.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("stringupdate".equals(evt.getPropertyName())) {
                    System.out.println((String) evt.getNewValue());
                    njf.jDataPayload.setText((String) evt.getNewValue());
                }
            }
        });
        worker.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("rxcommupdate".equals(evt.getPropertyName())) {
                    evtRxCommPack = (RxCommPack) evt.getNewValue();

                    StringBuffer addr = new StringBuffer();
                    StringBuffer com  = new StringBuffer();
                    StringBuffer len  = new StringBuffer();
                    StringBuffer buf  = new StringBuffer();

                    Utils.byte2hex((byte) evtRxCommPack.cNetAddr, addr);
                    Utils.byte2hex(evtRxCommPack.cCommand, com);
                    Utils.byte2hex(evtRxCommPack.cLenExpected, len);

                    for (int i = 0; i < (int) evtRxCommPack.cLenExpected; i++) {
                        Utils.byte2hex(evtRxCommPack.c485Buf[i], buf);
                    }

                    // String cmd = new String(a);
                    njf.jTextCmd.setText(com.toString());
                    njf.jTextLen.setText(len.toString());
                    njf.jDataPayloadHex.setText(buf.toString());

                    TableModel tblm = njf.jTable.getModel();

                    tblm.setValueAt(addr.toString(), 0, 0);
                    tblm.setValueAt(com.toString(), 0, 1);
                    tblm.setValueAt(len.toString(), 0, 2);
                    tblm.setValueAt(buf.toString(), 0, 3);

                }
            }
        });

        Rs485 rs485 = new Rs485((char) 513, worker);
    }

    void setStringUpdate(String stringUpdate) {
        if (stringUpdate.equals(this.stringUpdate)) {
            return;
        }

        String oldstringUpdate = this.stringUpdate;

        this.stringUpdate = stringUpdate;

        if (getPropertyChangeSupport().hasListeners("stringupdate")) {
            firePropertyChange("stringupdate", oldstringUpdate,
                               this.stringUpdate);
        }
    }

    void setRxPackUpdate(RxCommPack rxCommPack) {
        if (rxCommPack == this.rxCommPack) {
            return;
        }

        RxCommPack oldrxCommPack = this.rxCommPack;

        this.rxCommPack = rxCommPack;

        if (getPropertyChangeSupport().hasListeners("rxcommupdate")) {
            firePropertyChange("rxcommupdate", oldrxCommPack, this.rxCommPack);
        }
    }

    public RxCommPack getRxPackUpdate() {
        return rxCommPack;
    }

    public String getStringUpdate() {
        return stringUpdate;
    }
}
